<?php defined('SYSPATH') or die(); ?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>销售策略列表-思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_js'}
    {php echo Common::getScript("choose.js,product_add.js"); }
    {php echo Common::getCss('style.css,base.css,base2.css,base_new.css,plist.css,comment_dialog_product_list.css'); }

</head>
<body style="overflow:hidden">
<div class="s-main">
    <div class="search-bar filter" id="search_bar">
        <input type="text" id="searchkey" value="" placeholder="策略名称" class="sty-txt1 set-text-xh wid_200 mt-4 ml-10" style="color: rgb(170, 170, 170);">
        <a href="javascript:;" class="fl btn radius btn-secondary size-S mt-5 ml-5" onclick="goSearch()">搜索</a>
        <div class="fr cgs-info mr-20">
            <span class="cgs-hd fl f-12 c-666 l-30">针对采购商：</span>
            <div class="cgs-bd l-30 fl">
                <label class="radio-label fl all-purchaser"><input type="radio" {if $status==2}checked="checked"{/if} name="discount_type" value="1">全部</label>
                <label class="radio-label fl ml-10 part-purchaser"><input type="radio" {if $status==1}checked="checked"{/if} name="discount_type" value="2">部分</label>
                <a href="javascript:;" class="fl ml-5 c-primary" style="line-height: 30px" onclick="set_purchaser()">设置</a>
            </div>
        </div>
    </div>
    <div id="product_grid_panel" class="content-nrt">

    </div>
    <div class="save-con">
        <input type="hidden" name="purchaser_id" value="">
        <a href="javascript:;" data-id="{$id}" class="confirm-btn btn-danger" onclick="strategy_delete($(this))">删除策略</a>
        <a href="javascript:;" data-roomid="{$id}" class="confirm-btn determine-btn">确定</a>
    </div>
</div>

<script>
    var room_id = "{$id}";
    function strategy_delete(obj)
    {
        var purchaser_id = $("[name='purchaser_id']").val();
        var id = obj.attr('data-id');
        $.ajaxform({
            url   :  SITEURL+"ebooking/ajax_delete_strategy",
            method  :  "POST",
            data  : {id:id,type:2},
            dataType:'json',
            success  :  function(data)
            {
                if(data.status)
                {
                    ST.Util.showMsg('删除成功!','4',2000);
                }
                $('.product_check:checked').removeAttr('checked');

                ST.Util.responseDialog({id: id, show_status:1}, true);
            }
        },'JSON');
    }
    function set_purchaser()
    {
        $('.part-purchaser input').attr('checked','checked');
        var id = $(".product_check:checked").val();
        CHOOSE.setSome("设置采购商", {
            loadCallback: purchaser_id,
            maxHeight: 750,
            width:750
        }, SITEURL + 'purchaser/admin/credit/dialog_purchaser_list?status=all&sale_id='+id+'&room_id='+room_id, true);
    }
    function purchaser_id(result, bool)
    {
        var id = new Array();
        for (var i=0;i<result.purchaser_arr.length;i++){
            var row = result.purchaser_arr[i];
            id.push(row.id);
        }
        id = id.toString(',');
        $("[name='purchaser_id']").val(id);
    }
    Ext.onReady(
        function () {
            Ext.tip.QuickTipManager.init();

            $(".determine-btn").click(function () {
                var id = $(".product_check:checked").val();
                if (!id) {
                    ST.Util.showMsg('请选择策略', '5', 1000);
                    return;
                }
                var record = window.product_store.getById(id.toString());
                var title = record.get('name');
                var roomid = $(this).attr('data-roomid');
                var purchaser_id = $("[name='purchaser_id']").val();

                ST.Util.responseDialog({id: id, title: title, roomid:roomid, purchaser_id:purchaser_id}, true);
            });

            //产品store
            window.product_store = Ext.create('Ext.data.Store', {
                fields: [
                    'id',
                    'name',
                    'type',
                    'value_type',
                    'value',
                    'is_open',
                    'create_time',
                    'type_id',
                    'checked'
                ],
                proxy: {
                    type: 'ajax',
                    extraParams: {'virtual': '{$virtual}',room_id},
                    api: {
                        read: SITEURL + '/ebooking/ajax_get_sale_list',  //读取数据的URL
                        update: '',
                        destroy: ''
                    },
                    reader: {
                        type: 'json',   //获取数据的格式
                        root: 'lists',
                        totalProperty: 'total'
                    }
                },
                remoteSort: true,
                autoLoad: true,
                pageSize: 10,
                listeners: {
                    load: function (store, records, successful, eOpts) {
                        if (!successful) {
                            ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                        }
                        var pageHtml = ST.Util.page(store.pageSize, store.currentPage, store.getTotalCount(), 10);
                        $("#line_page").html(pageHtml);
                        window.product_grid.doLayout();

                        $(".pageContainer .pagePart a").click(function () {
                            var page = $(this).attr('page');
                            product_store.loadPage(page);
                        });
                    }
                }
            });

            //产品列表
            window.product_grid = Ext.create('Ext.grid.Panel', {
                store: product_store,
                renderTo: 'product_grid_panel',
                border: 0,
                bodyBorder: 0,
                bodyStyle: 'border-width:0px',
                layout: 'fit',
                scroll: 'vertical', //只要垂直滚动条
                bbar: Ext.create('Ext.toolbar.Toolbar', {
                    store: product_store,  //这个和grid用的store一样
                    displayInfo: true,
                    emptyMsg: "",
                    items: [
                        {
                            xtype: 'panel',
                            id: 'listPagePanel',
                            html: '<div id="line_page"></div>'
                        }
                    ],
                    listeners: {
                        single: true,
                        render: function (bar) {
                            var items = this.items;
                            bar.insert(0, Ext.create('Ext.toolbar.Fill'));
                        }
                    }
                }),
                columns: [
                    {
                        text: '策略名称',
                        width: '35%',
                        dataIndex: 'name',
                        align: 'center',
                        menuDisabled: true,
                        border: 0,
                        sortable: false
                    },
                    {
                        text: '策略类型',
                        width: '35%',
                        dataIndex: 'type',
                        align: 'center',
                        menuDisabled: true,
                        border: 0,
                        sortable: false,
                        renderer: function (value, metadata, record){
                            var value_type = record.get('value_type');
                            var number = record.get('value');
                            var str = '在同行采购价基础上';
                            str+= value==1?'增加':'减少';
                            str+= value_type==1?(number*100):number;
                            str+= value_type==1?'%':'元';
                            return str;
                        }
                    },
                    {
                        text: '选择',
                        width: '30%',
                        align: 'center',
                        border: 0,
                        sortable: false,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            var id = record.get('id');
                            var checked = record.get('checked');
                            console.log(checked);
                            if(checked){
                                return "<input type='radio' name='product_check' checked='checked' class='product_check' value='" + id + "'/>";
                            }else{
                                return "<input type='radio' name='product_check' class='product_check' value='" + id + "'/>";
                            }
                        }
                    }
                ],
                listeners: {
                    boxready: function () {
                    },
                    afterlayout: function (grid) {
                        ST.Util.resizeDialog('.s-main');
                        var data_height = 0;
                        try {
                            data_height = grid.getView().getEl().down('.x-grid-table').getHeight();
                        } catch (e) {
                        }
                        var height = Ext.dom.Element.getViewportHeight();

                        if (data_height > height - 65) {
                            window.has_biged = true;
                            grid.height = height - 65;
                        } else if (data_height < height - 65) {
                            if (window.has_biged) {
                                window.has_biged = false;
                                grid.doLayout();
                            }
                        }
                    }
                }
            });
        })

    //实现动态窗口大小
    Ext.EventManager.onWindowResize(function () {

    })

    function goSearch() {
        var keyword = $.trim($("#searchkey").val());
        window.product_store.getProxy().setExtraParam('keyword', keyword);
        window.product_store.loadPage(1);
    }
</script>

</body>
</html>
